package cn.edu.gzy.web.server.apis;

import cn.edu.gzy.web.server.service.MovieInfoService;
import cn.edu.gzy.web.server.service.dto.MovieInfo;
import cn.edu.gzy.web.server.utils.Page;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author guzy
 * @Description 影片表现层
 * @Date 2022/4/26 14:08
 **/

@RestController
@RequestMapping("/api/movie-infos")
public class MovieInfoApi {

    @Autowired
    MovieInfoService movieInfoService;


    /**
     * 分页获取 全部影片
     * 可以根据关键字获取影片
     *
     * @param page 页码
     * @param size 页长
     * @return Page<MoviesInfo>
     */
    @GetMapping
    public ResponseEntity getAllMovies(
            //默认值 和 非必填
            @RequestParam(required = false) String keywords,
            @RequestParam(required = false, defaultValue = "0") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        try {
            Page<MovieInfo> result;
            if (keywords == null) {
                //调用service业务逻辑
                result = movieInfoService.getAllMovieInfo(page, size);
            } else {
                //调用service业务逻辑
                result = movieInfoService.getMoviesByKeywords(keywords, page, size);
            }
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            //alibaba json
            JSONObject error = new JSONObject();
            error.put("errorMessage", e.getMessage());
            return ResponseEntity.internalServerError().body(error);
        }
    }


    /**
     * 根据id获取影片信息
     *
     * @param movieId 影片id
     * @return MovieInfo
     */
    @GetMapping("{movieId}")
    public ResponseEntity getMovieInfoById(@PathVariable Long movieId) {
        try {
            //调用service业务逻辑
            MovieInfo result = movieInfoService.getMovieById(movieId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            //alibaba json
            JSONObject error = new JSONObject();
            error.put("errorMessage", e.getMessage());
            return ResponseEntity.internalServerError().body(error);
        }
    }


    /**
     * 发布一部影片
     *
     * @param movieInfo
     * @return MovieInfo
     */
    @PostMapping
    public ResponseEntity publishMovie(@RequestBody MovieInfo movieInfo) {
        try {
            //校验movieInfo的id字段
            if (movieInfo != null && movieInfo.getMovieId() != null) {
                JSONObject error = new JSONObject();
                error.put("errorMessage", "新增影片不需要指定的movieId值");
                return ResponseEntity.badRequest().body(error);
            }
            //调用service业务逻辑
            MovieInfo result = movieInfoService.publishMovieInfo(movieInfo);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            //alibaba json
            JSONObject error = new JSONObject();
            error.put("errorMessage", e.getMessage());
            return ResponseEntity.internalServerError().body(error);
        }
    }


    /**
     * 修改影片信息
     *
     * @param movieInfo
     * @return MovieInfo
     */
    @PutMapping
    public ResponseEntity modifyMovie(@RequestBody MovieInfo movieInfo) {
        try {
            //校验movieInfo的id字段
            if (movieInfo == null || movieInfo.getMovieId() == null) {
                JSONObject error = new JSONObject();
                error.put("errorMessage", "修改影片需要传入指定的movieId值");
                return ResponseEntity.badRequest().body(error);
            }
            //调用service业务逻辑
            MovieInfo result = movieInfoService.modifyMovieInfo(movieInfo);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            //alibaba json
            JSONObject error = new JSONObject();
            error.put("errorMessage", e.getMessage());
            return ResponseEntity.internalServerError().body(error);
        }
    }


    /**
     * 删除一部影片
     *
     * @param movieId
     * @return 删除成功
     */
    @DeleteMapping("{movieId}")
    public ResponseEntity deleteMovie(@PathVariable Long movieId) {
        try {
            //调用service业务逻辑
            Boolean resultFlag = movieInfoService.deleteMovieInfo(movieId);
            JSONObject result = new JSONObject();
            result.put("flag", resultFlag);
            result.put("message", resultFlag ? "删除成功" : "删除失败");
            if (resultFlag) {
                return ResponseEntity.ok(result);
            } else {
                return ResponseEntity.internalServerError().body(result);
            }
        } catch (Exception e) {
            //alibaba json
            JSONObject error = new JSONObject();
            error.put("errorMessage", e.getMessage());
            return ResponseEntity.internalServerError().body(error);
        }
    }

}
